// pages/analysis/analysis.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    answerArray : [],
    answerMatrix:[],
    rightAnswerArray: [],
    questionList:[],
    questionNum:0,
    maxHeight: "1150px",
    
    bookAbbr:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let app = getApp()
    let answerArray = app.globalData.answerArray
    let rightAnswerArray = app.globalData.rightArray
    let questionList = app.globalData.questionList
    let questionNum = answerArray.length
    let bookAbbr = options.bookAbbr
    let answerMatrix = new Array()

    //依据answerArray和rightAnswerArray生成答案矩阵
    for(let i = 0; i < questionNum; ++i){
      //0表示选错了，1表示选对了,-1表示没有选择
      let answers = [-1,-1,-1,-1]
      if(questionList[i].multiple == 0){
        let optionNum = 0
        switch(answerArray[i]){
          case 'A':
            optionNum = 0
            break
          case 'B':
            optionNum = 1
            break
          case 'C':
            optionNum = 2
            break
          case 'D':
            optionNum = 3
            break
        }
        if(answerArray[i] == rightAnswerArray[i]){
          answers[optionNum] = 1
        }else{
          answers[optionNum] = 0
        }
      }else{
        let arr = answerArray[i].split("")
        for(let j = 0; j < arr.length; ++j){
          let optionNum = 0
          switch(arr[j]){
            case 'A':
              optionNum = 0
              break
            case 'B':
              optionNum = 1
              break
            case 'C':
              optionNum = 2
              break
            case 'D':
              optionNum = 3
              break
          }
          if(rightAnswerArray[i].indexOf(arr[j]) != -1){
            //这个选项选对了
            answers[optionNum] = 1
          }else{
            answers[optionNum] = 0
          }
        }
      }
      answerMatrix.push(answers)
    }

    this.setData({
      answerArray:answerArray,
      rightAnswerArray:rightAnswerArray,
      questionList:questionList,
      questionNum:questionNum,
      answerMatrix:answerMatrix,
      bookAbbr:bookAbbr,
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    setTimeout(() => {
      var query = wx.createSelectorQuery();
      query.select('.container').boundingClientRect()
      query.exec((res) => {
        this.setData({maxHeight:res[0].height + "px"})
      })
    }, 500)
  },
  adjustPage: function(e){
    let current = e.detail.current
    if(current != this.data.questionNum){
      this.setData({maxHeight:"1350px"})
      setTimeout(() => {
        var query = wx.createSelectorQuery();
        query.select('.container' + current).boundingClientRect()
        query.exec((res) => {
          
          this.setData({maxHeight:res[0].height + "px"})

        })
      }, 500)
    }else{
      let height = wx.getSystemInfoSync().windowHeight;
      this.setData({maxHeight:height + "px"})
    }
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  goToCatalog:function(){
    let app = getApp()
    let id = app.globalData.questionList[0].bookId
    
    wx.navigateTo({
      url: '/pages/catalog/catalog?bookId='+id+"&bookAbbr=" + this.data.bookAbbr,
    })
  },
  quit:function(){
    wx.switchTab({
      url: '/pages/home/home',
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})